package com.fruit.controller.app.goods;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.constants.GoodsContant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.goods.GoodsSpec;
import com.fruit.pojo.goods.InterfaceConfig;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.service.goods.GoodsManager;
import com.fruit.util.ResultInfo;
/**
 * 商品controller
 * 
 * @author shibeilei
 *
 */
@Controller
@RequestMapping(value = "/app/goods/")
public class GoodsController extends BaseController {
	
	@Autowired
	private GoodsManager goodsManager;
	

	/**
	 * 认养首页
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> index(HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "认养首页-开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("position", GoodsContant.SEASONAL);//认养首页列表推荐
		pam.put("start", 0);
		pam.put("num", 6);
		try {
			//获取商品列表
			List<GoodsSpec> goodsList = goodsManager.getGoodsList(pam);//获取商品列表
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}

	
	
	/**
	 * 商品列表
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getGoods", method = {RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getGoods(Page page,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "商品首页加载更多");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("position", GoodsContant.SEASONAL);//认养首页列表推荐
		pam.put("start", (page.getPage()-1) * page.getShowNum());
		pam.put("num", page.getShowNum());
		try {
			//获取商品列表
			List<GoodsSpec> goodsList = goodsManager.getGoodsList(pam);//获取商品列表
			resultMap.put("goodsList",goodsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}

	/**
	 * 查询基地
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getBase", method = {RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getBase(Integer id,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "查询基地信息");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//获取商品列表
			Base BaseInfo = goodsManager.getBase(id);//获取基地信息
			resultMap.put("BaseInfo",BaseInfo);
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
	
	/**
	 * 获取首页按钮/个人中心按钮
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getInterfaceConfig", method = {RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getInterfaceConfig(String appType,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "按钮配置");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(StringUtils.isEmpty(appType)){
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "no-param", "缺少参数"));
			return resultMap;
		}
		try {
			//获取按钮配置
			List<InterfaceConfig> config = goodsManager.getInterfaceConfig(appType);
			if(config != null){
				resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
				resultMap.put("interfaceConfig",config);
			}else{
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", ""));
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
	
	/**
     * 首页权益配置
     *
     * @param id 权益信息ID
     * @return 权益信息详情
     */
    @RequestMapping(value = "/rightConfig", method = {RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> rightConfig(HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, " 权益配置");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<RightConfig> rc=goodsManager.getRightConfigList();
			if(rc.isEmpty()){
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "no", "未查询到数据"));
				return resultMap;
			}
			resultMap.put("rightConfig",rc);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
    }
}
